#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2018/7/11 21:18
# @Author  : hyang
# @File    : management.py
# @Software: python_utils

from core.main import FTPServer
from utils.single_log import SingletonLog


class ManageTool(object):
    """
    负责解析用户命令
    """

    def __init__(self, sys_argv):
        """
         初始化
        :param sys_argv:
        """
        self.sys_argv = sys_argv
        self.log = SingletonLog().get_logger()
        self.log.info('参数{}'.format(self.sys_argv))
        self.verify_argv()

    def verify_argv(self):
        """
        验证指令
        :return:
        """
        if len(self.sys_argv) < 2:
            self.log.warn('参数长度应该大于1')
            self.help_msgs()
        else:
            cmd = self.sys_argv[1]
            if not hasattr(self, cmd):
                self.log.warn('invalid arugment')
                self.help_msgs()
            else:
                self.execute()

    def help_msgs(self):
        msg = '''
        start  start FTP Server
        '''
        print(msg)

    def execute(self):
        """
        解析指令
        :return:
        """
        cmd = self.sys_argv[1]
        func = getattr(self, cmd)
        func()

    def start(self):
        """start Ftp Server"""
        server = FTPServer(self)
        server.run_forever()
